<template>
  <div>
    <audio ref="audio" :src="url" controls autoplay style="width:100%;"></audio>
  </div>
</template>

<script>

import COS from 'cos-js-sdk-v5';
import { imgUploadInitCos } from '../../api/index';

export default {
  data() {
    return {
      url: '',
    };
  },
  methods: {
    playCOSWAVFile(key) {
      if (!this.cos) { // 如果 COS 对象为空，则创建该对象
        const CosConfig = {
          // 必选参数
          getAuthorization: function(options, callback) {
            imgUploadInitCos({
              BUCKET_NAME: 'daidaiwang-1307355890',
              REGION_NAME: 'ap-beijing'
            }).then(res => {
              if (res.code == 0) {
                var credentials = res.config && res.config.credentials;
                if (!res.config || !credentials) return console.error('credentials invalid');
                callback({
                  TmpSecretId: credentials.tmpSecretId,
                  TmpSecretKey: credentials.tmpSecretKey,
                  XCosSecurityToken: credentials.sessionToken,
                  StartTime: res.config.startTime, // 时间戳，单位秒，如：1580000000
                  ExpiredTime: res.config.expiredTime // 时间戳，单位秒，如：1580000900
                });
              }
            });
          }
        };
        this.cos = new COS(CosConfig);
      }
      this.url = this.cos.getObjectUrl({
        Bucket: 'daidaiwang-1307355890',
        Region: 'ap-beijing',
        Key: key,
        Sign: true,
        'response-content-type': 'audio/mp3',
      }, (err, data) => {
        // console.log(data)
      });
    },


  },
  mounted() {
  }
};
</script>